#@layout()
#define main()
<fieldset class="solid">
	<legend>构建工程: #(tbProject.title??)</legend>
	<div id="scriptOutput">
		#(tbBuild.triggerDesc)
	</div>
</fieldset>
<script type="text/javascript">
	console.log("javascript work...");
	/*setInterval(function(){
		$("#scriptOutput").append("git pull"+"<br/>");
	}, 1000);*/
	//默认向下滚动 看到最新 追加的内容，通过判断 鼠标滚轮 滑动数量，智能判断 是否真的想看 最新的内容
var down = true;
	var socket;

		function login(fromId){
            if(!window.WebSocket){return;}
            if(socket.readyState == WebSocket.OPEN){
                var jsonObject = JSON.parse("{}");
                jsonObject["login"] = fromId;
                socket.send(JSON.stringify(jsonObject));
                joinGroup("build_group_#(tbBuild.id)",fromId,1);
            }else{
                alert("WebSocket 连接没有建立成功！");
            }
        }
        //加入群组
        function joinGroup(groupId,userId,type){
            if(!window.WebSocket){return;}
            if(socket.readyState == WebSocket.OPEN){
                var jsonObject = JSON.parse("{}");
                jsonObject["groupId"] = groupId;
                jsonObject["userId"] = userId;
                jsonObject["type"] = type;
                socket.send(JSON.stringify(jsonObject));
            }else{
                alert("WebSocket 连接没有建立成功！");
            }
        }

        if(!window.WebSocket){
            window.WebSocket = window.MozWebSocket;
        }
        if(window.WebSocket){
            socket = new WebSocket("ws://#(serverIp):12345/ws");
            socket.onmessage = function(event){
                var outDiv = document.getElementById('scriptOutput');
                //ta.value += event.data+"\r\n";
                $("#scriptOutput").append(event.data+"<br/>");

				//随着输出增加 滚动到底部
                //outDiv.scrollTop = outDiv.scrollHeight;
                if(down){
                	$(window).scrollTop($(document).height()-$(window).height());
                }

            };
            socket.onopen = function(event){
                var ta = document.getElementById('scriptOutput');
                ta.value = "Netty-WebSocket服务器。。。。。。连接  \r\n";
                var uid = "#(session.uid.id)";
                login(uid);
            };
            socket.onclose = function(event){
                var ta = document.getElementById('scriptOutput');
                ta.value = "Netty-WebSocket服务器。。。。。。关闭 \r\n";
            };
        }else{
            alert("您的浏览器不支持WebSocket协议！");
        }

		var downCount = 0;
        document.onmousewheel = function(e) {
        e = e || window.event;
        var wheelDelta = e.wheelDelta;
        if (wheelDelta > 0) {
        	down = false;
            console.log("鼠标向上滚动");
        } else {
        	downCount++;
        	if(downCount > 200){
				down = true;
				downCount = 0;
				console.log("向下幅度 累计到200,变为自动追加");
        	}
            console.warn("鼠标向下滚动");
        }
    }
</script>
#end